/*
XBaseTools
Copyright (C) 2008 Matthew Perry

This library/program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This libary/program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;

namespace BaseTools.Configuration
{
	/// <summary>
	/// A source configuration pair/value
	/// </summary>
	public class ConfigPair : ICloneable
	{
		/// <summary>
		/// The key
		/// </summary>
		public string Key;
		
		/// <summary>
		/// The data
		/// </summary>
		public Object Data;
		
		/// <summary>
		/// Whether key/data is quoted
		/// </summary>
		public bool KeyQuoted, DataQuoted;
		
		/// <summary>
		/// Creates a new pair
		/// </summary>
		/// <param name="key">The key of the pair</param>
		/// <param name="data">The data of the pair</param>
		public ConfigPair(string key, Object data)
		{
			this.Key = key;
			this.Data = data;
			this.KeyQuoted = true;
			this.DataQuoted = true;
		}
		
		/// <summary>
		/// Creates a new pair
		/// </summary>
		/// <param name="key">The key of the pair</param>
		/// <param name="data">The data of the pair</param>
		/// <param name="kq">Whether the key is quoted</param>
		/// <param name="dq">Whether the data is quoted</param>
		public ConfigPair(string key, Object data, bool kq, bool dq)
		{
			this.Key = key;
			this.Data = data;
			this.KeyQuoted = kq;
			this.DataQuoted = dq;
		}
		
		/// <summary>
		/// The data as a string
		/// </summary>
		public string DataAsString
		{
			get
			{
				if (Data is string)
					return (string)Data;
				else
					return null;
			}
			set
			{
				Data = value;
			}
		}
		
		/// <summary>
		/// The data as a list
		/// </summary>
		public ConfigList DataAsList
		{
			get
			{
				if (Data is ConfigList)
					return (ConfigList)Data;
				else
					return null;
			}
			set
			{
				Data = value;
			}
		}
		
		/// <summary>
		/// The data as an integer
		/// </summary>
		public int DataAsInteger
		{
			get
			{
				if (Data is int)
					return (int)Data;
				else if (Data is short)
					return (int)((short)Data);
				else
					return -1;
			}
			set
			{
				Data = value;
			}
		}
		
		/// <summary>
		/// The data as a double
		/// </summary>
		public double DataAsDouble
		{
			get
			{
				if (Data is double)
					return (double)Data;
				else if (Data is float)
					return (double)((float)Data);
				else
					return -1;
			}
			set
			{
				Data = value;
			}
		}
		
		/// <summary>
		/// Checks whether the data is a double
		/// </summary>
		/// <returns>Whether data is a double</returns>
		public bool DataIsDouble
		{
			get
			{
				return Data is double || Data is float;
			}
		}
		
		/// <summary>
		/// Checks whether the data is an integer
		/// </summary>
		/// <returns>Whether data is an integer</returns>
		public bool DataIsInteger
		{
			get
			{
				return Data is int || Data is short;
			}
		}
		
		/// <summary>
		/// Checks whether the data is a string
		/// </summary>
		/// <returns>Whether data is a string</returns>
		public bool DataIsString
		{
			get
			{
				return Data is string;
			}
		}
		
		/// <summary>
		/// Checks whether the data is a ConfigList
		/// </summary>
		/// <returns>Whether data is a ConfigList</returns>
		public bool DataIsList
		{
			get
			{
				return Data is ConfigList;
			}
		}
		
		/// <summary>
		/// Creates a clone of this ConfigPair
		/// </summary>
		/// <returns>A clone of this ConfigPair</returns>
		public Object Clone()
		{
			if (Data is ICloneable && !(Data is string))
				return new ConfigPair(Key, ((ICloneable)Data).Clone(), KeyQuoted, DataQuoted);
			else
				return new ConfigPair(Key, Data, KeyQuoted, DataQuoted);
		}
	}
}
